//
//  MTClubSearchBarView.swift
//  MT
//
//  Created by ooops on 2020/5/9.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol MTClubSearchBarViewDelegate: NSObjectProtocol {
    func textFieldShouldBeginEditing(_ textField: UITextField)
}

class MTClubSearchBarView: UIView {
    
    weak var delegate: MTClubSearchBarViewDelegate?
    
    var title: String? {
        didSet {
            tipLabel.text = title
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    private func setupUI() {
        addSubview(contentTextField)
        addSubview(searchIconImage)
        addSubview(tipLabel)
    }
    
    private func makeConstraint() {
        contentTextField.snp.makeConstraints { (make) in
            //make.edges.equalTo(UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10))
            make.edges.equalToSuperview()
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.centerX.equalTo(self.snp.centerX).offset(18)
        }
        
        searchIconImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(tipLabel.snp.left).offset(-5)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    

    lazy var contentTextField: UITextField = {
        let txf = UITextField()
        txf.delegate = self
        txf.textColor = UIColor.text.x9b9b9b
        txf.font = PingFangSCMediumFont(14)
        txf.layer.borderColor = UIColor(hex: 0xe6e6e6).cgColor
        txf.layer.cornerRadius = 4
        txf.layer.masksToBounds = true
        txf.backgroundColor = .white
        txf.layer.borderWidth = 1
        return txf
    }()
    
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(14)
        l.textColor = UIColor.text.x9b9b9b
        l.text = "觅颜号/手机号"
        return l
    }()
    
    lazy var searchIconImage: UIImageView = {
        let imageV = UIImageView()
        imageV.image = UIImage(named: "search")
        return imageV
    }()

}

extension MTClubSearchBarView: UITextFieldDelegate {
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.delegate?.textFieldShouldBeginEditing(textField)
        return true
    }
    
}
